package wens.pig.base.plugin.form;

import com.wens.common.core.eas.EasOp;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import wens.pig.base.utils.DateUtil;
import wens.pig.base.utils.ObjectUtil;

import java.sql.Types;
import java.util.Date;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * 定价申请单表单插件
 *
 * @author WSP
 * @path 种猪生产->存栏管理->首页
 * @since 2023年05月22日-10:02
 */
public class PriceApplyFormPlugin extends AbstractFormPlugin {
    private static final String ENTRY = "wens_entryentity";
    @Override
    public void registerListener(EventObject e) {
        this.addItemClickListeners("wens_toolbarap");
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        FormShowParameter parameter = this.getView().getFormShowParameter();
        String productId = parameter.getCustomParam("productId");
        String bizDate = parameter.getCustomParam("bizDate");
        String sex = parameter.getCustomParam("sex");
        String toOrgId = ObjectUtil.defaultIfNull(parameter.getCustomParam("toOrgId"), "");
        String genId = ObjectUtil.defaultIfNull(parameter.getCustomParam("genId"), "");
        String fromOrgId = parameter.getCustomParam("fromOrgId");
        String colony = parameter.getCustomParam("colony");
        String financeType = parameter.getCustomParam("financeType");
        String cusId = parameter.getCustomParam("cusId");

        EasOp.Param param = EasOp.Param.create(productId, Types.VARCHAR);
        param.add(bizDate, Types.VARCHAR);
        param.add(bizDate, Types.VARCHAR);
        param.add(fromOrgId, Types.VARCHAR);
        param.add(sex, Types.VARCHAR);

        String sql = "SELECT a.fnumber billno, a.cfdemanddate needdate, org.fname_l2 org, \n" +
                " (CASE WHEN a.cfsalebyway='11' THEN '按头销售' WHEN a.cfsalebyway = '201' THEN '按基础重和肉猪指导价折算' \n" +
                "  WHEN a.cfsalebyway = '202' THEN '按基础重和固定斤价计算' ELSE '按斤销售' END) saletype \n" +
                "FROM ct_bil_pigsalepriceadjust a \n" +
                "LEFT JOIN t_org_baseunit org ON a.cftoorgunitwensid = org.fid \n" +
                "WHERE a.cfifauditing = 1 AND a.cfproductid = ? \n" +
                "AND TO_DATE(?, 'yyyy-mm-dd') > TRUNC(a.cfdemanddate, 'DD') -4 AND TO_DATE(?, 'yyyy-mm-dd') < TRUNC(a.cfdemanddate,'DD') + 4 \n" +
                "AND a.cffromorgunitwensi = ? \n" +
                "AND (a.cfsex = ? OR a.cfsex='2') ";

        if (StringUtils.isNotBlank(toOrgId)) {
            sql += "AND (a.cftoorgunitwensid = ? OR a.cftoorgunitwensid is null) \n";
            param.add(toOrgId, Types.VARCHAR);
        }
        if (StringUtils.isNotBlank(financeType)) {
            sql += "AND a.cffinancetype = ? ";
            param.add(financeType, Types.VARCHAR);
        }
        if (StringUtils.isNotBlank(genId)) {
            sql += "AND (a.cfgeneticsid = ? OR a.cfgeneticsid is null) ";
            param.add(genId, Types.VARCHAR);
        }
        if (StringUtils.isNotBlank(colony)) {
            sql += "AND (a.cfcolony = ? or a.cfcolony = 2)";
            param.add(colony, Types.VARCHAR);
        }
        if (StringUtils.isNotBlank(cusId)) {
            sql += "AND a.cfcustomerid = ?";
            param.add(cusId, Types.VARCHAR);
        }

        List<Map<String, Object>> queryList = EasOp.query(sql, param);
        this.getModel().deleteEntryData(ENTRY);
        for (Map<String, Object> data : queryList) {
            int index = this.getModel().createNewEntryRow(ENTRY);
            this.getModel().setValue("wens_billno", data.get("billno"), index);
            this.getModel().setValue("wens_saletype", data.get("saletype"), index);
            this.getModel().setValue("wens_org", data.get("org"), index);
            if (data.get("needdate") != null) {
                this.getModel().setValue("wens_needdate", DateUtil.dateToString((Date) data.get("needdate"), DateUtil.DATE_FORMAT_YYYY_MM_DD), index);
            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        if ("wens_submit".equals(evt.getItemKey())) {
            EntryGrid entryGrid = this.getControl(ENTRY);
            int[] selectRows = entryGrid.getSelectRows();
            if (selectRows == null || selectRows.length == 0) {
                this.getView().showTipNotification("请选择一行数据。");
                return;
            }
            DynamicObject resultData = this.getModel().getEntryRowEntity(ENTRY, selectRows[0]);
            this.getView().returnDataToParent(resultData);
            this.getView().close();
        }
    }
}